/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    system.c
 * @date:   Feb 12, 2012
 */

#include "system.h"

/* private code declaration */

/* public code definition */
char* installer_system_get_system_name() {
    struct utsname *name = (struct utsname*) malloc(sizeof(struct utsname));
    char           *system_name;

    if (name == NULL) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_system_name", strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* put information about the system in name */
    if (uname(name) < 0) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_system_name", strerror(errno));
        exit(EXIT_FAILURE);
    }
    
    system_name = strdup(name->sysname);

    free(name);

    return system_name;
}

char* installer_system_get_node_name() {
    struct utsname *name = (struct utsname*) malloc(sizeof(struct utsname));
    char           *node_name;

    if (name == NULL) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_node_name", strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* put information about the system in name */
    if (uname(name) < 0) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_node_name", strerror(errno));
        exit(EXIT_FAILURE);
    }
    
    node_name = strdup(name->nodename);

    free(name);

    return node_name;

}

char* installer_system_get_os_release() {
    struct utsname *name = (struct utsname*) malloc(sizeof(struct utsname));
    char           *release;

    if (name == NULL) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_os_release", strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* put information about the system in name */
    if (uname(name) < 0) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_os_release", strerror(errno));
        exit(EXIT_FAILURE);
    }
    
    release = strdup(name->release);

    free(name);

    return release;

}

char* installer_system_get_version() {
    struct utsname *name = (struct utsname*) malloc(sizeof(struct utsname));
    char           *version;

    if (name == NULL) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_version", strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* put information about the system in name */
    if (uname(name) < 0) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_version", strerror(errno));
        exit(EXIT_FAILURE);
    }
    
    version = strdup(name->version);

    free(name);

    return version;

}

char* installer_system_get_machine() {
    struct utsname *name = (struct utsname*) malloc(sizeof(struct utsname));
    char           *machine;

    if (name == NULL) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_machine", strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* put information about the system in name */
    if (uname(name) < 0) {
	fprintf(stderr, "[ERROR|%s] %s\n",
		"installer_system_get_machine", strerror(errno));
        exit(EXIT_FAILURE);
    }
    
    machine = strdup(name->machine);

    free(name);

    return machine;

}

/* private code definition */
